package com.spring.demo2.eg1;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;

/**
 * @author vhans
 */
public class Test {

    @org.junit.Test
    public void testYesConfiguration() {
        /*
        被@Configuration修饰的类，spring容器中会通过cglib给这个类创建一个代理，代理会拦截所有被@Bean修饰的方法，
        默认情况(bean为单例)下确保这些方法只被调用一次，从而确保这些bean是同一个bean，即单例的。
         */
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigBean.class);
        for (String beanName : context.getBeanDefinitionNames()) {
            String[] aliases = context.getAliases(beanName);
            System.out.printf("bean对象:%s,%n bean名称:%s,bean别名:%s%n",
                    context.getBean(beanName), beanName, Arrays.asList(aliases));
        }
    }

    @org.junit.Test
    public void testNoConfiguration() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(NoConfigBean.class);
        for (String beanName : context.getBeanDefinitionNames()) {
            String[] aliases = context.getAliases(beanName);
            System.out.printf("bean对象:%s,%n bean名称:%s,bean别名:%s%n",
                    context.getBean(beanName), beanName, Arrays.asList(aliases));
        }
    }
}
